using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Authentication;
using System.Xml;
using System.Data;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace SSLScavenger
{
        
    class Program
    {

        [STAThread()]
        static void Main(string[] args)
        {
            if (args.Length < 2) { Console.WriteLine("Start IP address and number of IPs shall be specified"); return; }
            if (string.IsNullOrEmpty(args[0])) Console.WriteLine("Start IP address is missing");
            if (string.IsNullOrEmpty(args[1])) Console.WriteLine("Number of Ips shall be specified");
            string strServerName = "";
            StringCollection Ranges = new StringCollection();
            Ranges.Add(args[0]);
            int totalIPs = Int32.Parse(args[1]);
            Wrapper obj = new Wrapper();
            foreach (string Range in Ranges)
            {
                int startIP = int.Parse(Regex.Match(Range, @"^.*\.(.*)$").Groups[1].ToString());
                string subnet = Regex.Match(Range, @"(^.*)\.(.*)$").Groups[1].ToString();
                for (int i = startIP; (i <= totalIPs + startIP && i <= 254); i++)
                {
                    strServerName = subnet + "." + i.ToString();
                    obj.Produce(new Ware(strServerName));
                }

                while (obj.QueueLength != 0)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }
            System.Windows.Forms.SaveFileDialog myDialog = new System.Windows.Forms.SaveFileDialog();
            myDialog.InitialDirectory = System.Windows.Forms.Application.StartupPath.ToString();
            myDialog.FileName = String.Format("{0:D} {0:hh.mm.ss} {0:ffff}", DateTime.Now) + ".xml";
            myDialog.ShowDialog();
            obj.DB.dstResults.WriteXml(myDialog.FileName);
        }
              
    }

    }
